# -*- coding: utf-8 -*-

class VendorController < ApplicationController
  before_filter :employee_authorize
  before_filter :sys_before_log
  before_filter :employee_action_hx
  before_filter :privilege_filter,:except=>[:vendorLock]

  #供应商页面
  def vendorList
    
  end

  #显示添加新供应商页面
  def newVendor

  end

  #显示编辑供应商页面
  def editVendor

  end
  
  def getAllVendors
    forCBB = params[:for_cbb]
    if forCBB != 'true'
      forCBB = 'false'
    end
    vendors = Vendor.name_like params[:query].to_s,forCBB
    jsonStr="{totalProperty:"+vendors.length.to_s+",root:"+vendors.to_json()+"}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  def create
    
    begin
      vendor = Vendor.new()
      vendor.company_id = session[:company_id]
      vendor.number = params[:number]
      vendor.name = params[:name]
      vendor.phone = params[:phone]
      vendor.fax = params[:fax]
      vendor.address = params[:address]
      vendor.linkman = params[:linkman]
      vendor.email = params[:email]
      vendor.bank = params[:bank]
      vendor.bank_account = params[:bank_account]
      vendor.description = params[:description]
      vendor.save!
      info = "成功"
    rescue Exception => e
      info = e
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #编辑前的数据准备
  def edit    
    vendor = Vendor.find(params[:id])
    rootStr = vendor.to_json()
    jsonStr = "{root:["+rootStr+"]}"
    render :text=>jsonStr, :layout=>false
  end

  #把编辑后的数据，更新到数据库中
  def update
    
    begin
      vendor = Vendor.find(params[:id])
      vendor.number = params[:number]
      vendor.name = params[:name]
      vendor.phone = params[:phone]
      vendor.fax = params[:fax]
      vendor.address = params[:address]
      vendor.linkman = params[:linkman]
      vendor.email = params[:email]
      vendor.bank = params[:bank]
      vendor.bank_account = params[:bank_account]
      vendor.description = params[:description]
      vendor.save!
      info = "成功"
    rescue Exception => e
      info = e.to_s
      DbLog.info(session[:company_id], session[:department_id], session[:employee_id], params[:controller], params[:action], request.remote_ip ,e.to_s)
    end
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end

  #锁定或解锁销售类型
  def vendorLock
    vendor = Vendor.find(params[:id])
    if vendor.is_locked == 1
      vendor.is_locked = 0
    else
      vendor.is_locked = 1
    end
    vendor.save!
    info = "成功"
    result = "{success:true,info:'#{info}'}"
    render :text=>result, :layout=>false
  end
end
